var db = require('../util/db')

//判断用户是否已关注
getUserIsFollowed = (user_id, follow_id) => {
    db.sqlConnect('select * from follow where user_id = ? and follow_id = ?', [user_id, follow_id], (err, result) => {
        if (err) {
            console.log(err)
            return
        }
        if (result?.affectedRows != 0) {
            return true
        }
        return false
    })
}

//用户关注接口
getUserFollowed = (req, res) => {
    let { user_id, follow_id } = req.body

    if (!getUserIsFollowed) {
        db.sqlConnect('insert into follow(user_id,follow_id) values(?,?)', [user_id, follow_id], (err, result) => {
            if (err) {
                console.log(err)
                return
            }
            if (result?.affectedRows != 0) {
                return res.send({
                    code: '200',
                    msg: '用户关注成功'
                })
            }
            return res.send({
                code: '200',
                msg: '用户关注失败'
            })
        })
    } else {
        return res.send({
            code: '200',
            msg: '用户已关注'
        })
    }

}